package net.toocruel.iqismart.web.controller.admin;

import net.toocruel.iqismart.entity.dto.SettingCreditAndCoinInfo;
import net.toocruel.iqismart.entity.setting.*;
import net.toocruel.iqismart.service.SettingService;
import net.toocruel.iqismart.service.exception.BusinessException;
import net.toocruel.iqismart.service.exception.UserRepeatException;
import net.toocruel.iqismart.web.controller.base.AbstractBaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/4/28 上午11:55
 */
@Controller
@RequestMapping("admin/setting")
public class AdminSettingController extends AbstractBaseController {
    private static String PREFIX = "/admin/setting/";

    @Autowired
    private SettingService settingService;

    @GetMapping("credits")
    public ModelAndView credits(HttpServletRequest request, HttpServletResponse response){
        SettingCreditAndCoinInfo settingCreditAndCoinInfo = settingCreditAndCoinInfo = new SettingCreditAndCoinInfo();
        settingCreditAndCoinInfo.setCredit(settingService.getSetting().getCredit());
        settingCreditAndCoinInfo.setCoin(settingService.getSetting().getCoin());

        return new ModelAndView(PREFIX+"credits",
                "settingCreditAndCoinInfo",
                settingCreditAndCoinInfo);
    }

    @PostMapping("credits")
    public String docredits(@Valid SettingCreditAndCoinInfo settingCreditAndCoinInfo, BindingResult bindingResult,
                            HttpServletRequest request, HttpServletResponse response){
        if(!bindingResult.hasErrors()){
            try {
                settingService.update(settingCreditAndCoinInfo);
                return  "redirect:"+PREFIX+"credits";
            } catch (UserRepeatException e) {
                e.printStackTrace();
                request.setAttribute("error",e);
            }
        }
        return  PREFIX+"credits";
    }

    @GetMapping("email")
    public ModelAndView email(HttpServletRequest request, HttpServletResponse response){
        return new ModelAndView(PREFIX+"email",
                "settingEmail",
                settingService.getSetting().getEmail());
    }

    @PostMapping("email")
    public String doEmail(@Valid SettingEmail settingEmail, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response){
        if(!bindingResult.hasErrors()){
            try {
                settingService.update(settingEmail);
                return  "redirect:"+PREFIX+"email";
            } catch (UserRepeatException e) {
                e.printStackTrace();
                request.setAttribute("error",e);
            }
        }
        return  PREFIX+"email";
    }

    @GetMapping("irrigation")
    public ModelAndView irrigation(HttpServletRequest request, HttpServletResponse response){
        return new ModelAndView(PREFIX+"irrigation",
                "settingIrrigation",
                settingService.getSetting().getIrrigation());
    }

    @PostMapping("irrigation")
    public String doirrigation(@Valid SettingIrrigation settingIrrigation,String codeCreateQuestion, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response){
        if(!bindingResult.hasErrors()){
            try {
                settingService.update(settingIrrigation);
                return  "redirect:"+PREFIX+"irrigation";
            } catch (UserRepeatException e) {
                e.printStackTrace();
                request.setAttribute("error",e);
            }
        }
        return  PREFIX+"irrigation";
    }

    @GetMapping("oauth")
    public ModelAndView oauth(HttpServletRequest request, HttpServletResponse response){
        String url = Setting.SETTING.getWebsite().getUrl();
        String pattern = "^((http://)|(https://))?([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}(/)";
        if(!Pattern.matches(pattern,url)){
            request.setAttribute("error",new BusinessException("配置域名后才能使用"));
        }

        return new ModelAndView(PREFIX+"oauth",
                "settingOauth",
                settingService.getSetting().getOauth());
    }

    @PostMapping("oauth")
    public String dooauth(@Valid SettingOauth settingOauth,BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response){
        if(!bindingResult.hasErrors()){
            try {
                settingService.update(settingOauth);
                return  "redirect:"+PREFIX+"oauth";
            } catch (UserRepeatException e) {
                e.printStackTrace();
                request.setAttribute("error",e);
            }
        }
        return  PREFIX+"oauth";
    }

    @GetMapping("register")
    public ModelAndView register(HttpServletRequest request, HttpServletResponse response){
        return new ModelAndView(PREFIX+"register",
                "settingRegister",
                settingService.getSetting().getRegister());
    }

    @PostMapping("register")
    public String doregister(@Valid SettingRegister settingRegister, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response){
        if(!bindingResult.hasErrors()){
            try {
                settingService.update(settingRegister);
                return  "redirect:"+PREFIX+"register";
            } catch (UserRepeatException e) {
                e.printStackTrace();
                request.setAttribute("error",e);
            }
        }
        return  PREFIX+"register";
    }

    @GetMapping("search")
    public ModelAndView search( HttpServletRequest request, HttpServletResponse response){
        return new ModelAndView(PREFIX+"search",
                "settingSearch",
                settingService.getSetting().getSearch());
    }

    @PostMapping("search")
    public String dosearch(@Valid SettingSearch settingSearch, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response){
        if(!bindingResult.hasErrors()){
            try {
                settingService.update(settingSearch);
                return  "redirect:"+PREFIX+"search";
            } catch (UserRepeatException e) {
                e.printStackTrace();
                request.setAttribute("error",e);
            }
        }
        return  PREFIX+"search";
    }

    @GetMapping("seo")
    public ModelAndView seo(HttpServletRequest request, HttpServletResponse response){
        return new ModelAndView(PREFIX+"seo",
                "settingSeo",
                settingService.getSetting().getSeo());
    }

    @PostMapping("seo")
    public String doseo(@Valid SettingSeo settingSeo, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response){
        if(!bindingResult.hasErrors()){
            try {
                settingService.update(settingSeo);
                return  "redirect:"+PREFIX+"seo";
            } catch (UserRepeatException e) {
                e.printStackTrace();
                request.setAttribute("error",e);
            }
        }
        return  PREFIX+"seo";
    }

    @GetMapping("time")
    public ModelAndView time(HttpServletRequest request, HttpServletResponse response){
        return new ModelAndView(PREFIX+"time",
                "settingTime",
                settingService.getSetting().getTime());
    }

    @PostMapping("time")
    public String dotime(@Valid SettingTime settingTime, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response){
        if(!bindingResult.hasErrors()){
            try {
                settingService.update(settingTime);
                return  "redirect:"+PREFIX+"time";
            } catch (UserRepeatException e) {
                e.printStackTrace();
                request.setAttribute("error",e);
            }
        }
        return  PREFIX+"time";
    }

    @GetMapping("variables")
    public String variables(HttpServletRequest request, HttpServletResponse response){
        return  PREFIX+"variables";
    }



    @GetMapping("website")
    public ModelAndView website(HttpServletRequest request, HttpServletResponse response){
        return new ModelAndView(PREFIX+"website",
                "settingWebsite",
                settingService.getSetting().getWebsite());
    }

    @PostMapping("website")
    public String doWebsite(@Valid SettingWebsite settingWebsite, BindingResult bindingResult, HttpServletRequest request, HttpServletResponse response){
        if(!bindingResult.hasErrors()){
            try {
                settingService.update(settingWebsite);
                return  "redirect:"+PREFIX+"website";

            } catch (UserRepeatException e) {
                e.printStackTrace();
                request.setAttribute("error",e);
            }
        }
        return  PREFIX+"website";
    }


//    @ModelAttribute("settingWebsite")
//    public SettingWebsite settingWebsite() {
//        return settingService.getSetting().getWebsite();
//    }
//
//    @ModelAttribute("settingCoin")
//    public SettingCoin settingCoin() {
//        return settingService.getSetting().getCoin();
//    }
//
//    @ModelAttribute("settingCredit")
//    public SettingCredit settingCredit() {
//        return settingService.getSetting().getCredit();
//    }
//
//    @ModelAttribute("settingEmail")
//    public SettingEmail settingEmail() {
//        return settingService.getSetting().getEmail();
//    }
//
//    @ModelAttribute("settingIrrigation")
//    public SettingIrrigation settingIrrigation() {
//        return settingService.getSetting().getIrrigation();
//    }
//
//    @ModelAttribute("settingOauth")
//    public SettingOauth settingOauth() {
//        return settingService.getSetting().getOauth();
//    }
//
//    @ModelAttribute("settingRegister")
//    public SettingRegister settingRegister() {
//        return settingService.getSetting().getRegister();
//    }
//
//    @ModelAttribute("settingSearch")
//    public SettingSearch settingSearch() {
//        return settingService.getSetting().getSearch();
//    }
//
//    @ModelAttribute("settingSeo")
//    public SettingSeo settingSeo() {
//        return settingService.getSetting().getSeo();
//    }
//
//    @ModelAttribute("settingTime")
//    public SettingTime settingTime() {
//        return settingService.getSetting().getTime();
//
//    }
//    @ModelAttribute("settingCreditAndCoinInfo")
//    public SettingCreditAndCoinInfo settingCreditAndCoinInfo() {
//        SettingCreditAndCoinInfo settingCreditAndCoinInfo = new SettingCreditAndCoinInfo();
//        settingCreditAndCoinInfo.setCredit(settingService.getSetting().getCredit());
//        settingCreditAndCoinInfo.setCoin(settingService.getSetting().getCoin());
//        return settingCreditAndCoinInfo;
//    }

    @ModelAttribute("currentTime")
    public Date currentTime() {
        return new Date();

    }
}
